package org.game.obj;

import io.netty.channel.Channel;
import org.common.mongo.collection.RoleCollection;
import org.common.mongo.collection.SimpleRoleCollection;
import org.common.netty.Pack;
import org.common.netty.PackCodec;
import org.game.module.item.ItemModule;
import org.game.module.role.RoleModule;
import org.protobuf.ProtoEnumMsg;

/**
 * 角色缓存
 */
public class RoleContext {

    /**
     * 角色基础信息
     */
    private SimpleRoleCollection simpleRoleCollection;
    /**
     * 角色账号
     */
    private long accountId;
    /**
     * 角色id
     */
    private long roleId;
    /**
     * 与登录服的通信channel
     */
    private Channel channel;

    /**
     * 角色全量数据
     */
    private RoleData roleData;

    /**
     * 角色模块
     */
    public RoleModule roleModule;

    /**
     * 道具模块
     */
    public ItemModule itemModule;

    /**
     * 角色初始化
     */
    public void initRoleContext(RoleCollection roleCollection) {
        this.roleData = new RoleData(roleCollection);
        // 模块初始化
        this.roleModule = new RoleModule(this);
        this.itemModule = new ItemModule(this);
    }

    public SimpleRoleCollection getSimpleRoleCollection() {
        return simpleRoleCollection;
    }

    public void setSimpleRoleCollection(SimpleRoleCollection simpleRoleCollection) {
        this.simpleRoleCollection = simpleRoleCollection;
    }

    public long getAccountId() {
        return accountId;
    }

    public void setAccountId(long accountId) {
        this.accountId = accountId;
    }

    public long getRoleId() {
        return roleId;
    }

    public void setRoleId(long roleId) {
        this.roleId = roleId;
    }

    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public RoleData getRoleData() {
        return roleData;
    }

    /**
     * 推送消息
     */
    public void push(Pack pack) {
        byte[] encode = PackCodec.encode(pack);
        channel.writeAndFlush(PackCodec.encode(ProtoEnumMsg.CMD.ID.PUSH_PROTO_TO_CLIENT_VALUE, encode));
    }
}
